package cn.cloudreal.cbms.castDetail.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cn.cloudreal.cbms.castDetail.dao.ChargeDao;
import cn.cloudreal.cbms.common.util.DatadicContext;
import cn.cloudreal.cbms.vo.Charge;
import cn.cloudreal.cbms.vo.Dictionary;
import cn.cloudreal.cbms.vo.FundBudget;
import cn.cloudreal.cbms.vo.LevelRule;
import cn.cloudreal.cbms.vo.MediaPrice;
import cn.cloudreal.cbms.vo.ProjectMedium;
import cn.cloudreal.cbms.vo.SourcePlanYear;

public class ChargeDaoImpl extends SqlMapClientDaoSupport implements ChargeDao
{

    /**
     * 取得预算使用折扣
     * 
     */
    public LevelRule getUnPromotionUsedRatio(Map condition)
    {
        Object obj = getSqlMapClientTemplate().queryForObject("charge.getUnPromotionUsedRatio", condition);
        return obj == null ? null : (LevelRule) obj;
    }

    public FundBudget getMediaSummarieByYd(Map condition)
    {
        Object obj = getSqlMapClientTemplate().queryForObject("charge.getMediaSummarieByYd", condition);
        return obj == null ? null : (FundBudget)obj;
    }

    public List<Dictionary> getCostTypeByProMedID(Map condition)
    {
        List<String> costList = getSqlMapClientTemplate().queryForList("charge.getCostTypeByProMedID", condition);
        return getDictionarys(costList);
    }

    public List<Dictionary> getSubCostTypeByProMedIDCostType(Map condition)
    {
        List<String> costList = getSqlMapClientTemplate().queryForList("charge.getSubCostTypeByProMedIDCostType", condition);
        return getDictionarys(costList);
    }
    
    private List<Dictionary> getDictionarys(List<String> ids)
    {
        if(ids == null || ids.isEmpty())
        {
            return null;
        }
        
        List<Dictionary> dics = new ArrayList<Dictionary>();
        for(String id : ids)
        {
            dics.add(DatadicContext.getInstance().getDatadicObject(id, "FYLX"));
        }
        
        return dics;
    }

    public void saveProjectMedioPrice(Charge charge)
    {
        getSqlMapClientTemplate().insert("charge.saveProjectMedioPrice", charge);
    }

    public SourcePlanYear getMediaSummariePcc(Map condition)
    {
        Object obj = null;
        try{
            obj = getSqlMapClientTemplate().queryForObject("charge.getMediaSummariePcc", condition);
        }catch (Exception e) {
            e.printStackTrace();
        }
        
        return obj == null ? null : (SourcePlanYear) obj;
    }

    public void updateMedioTotalPay(Map param)
    {
        getSqlMapClientTemplate().update("charge.updateMedioTotalPay", param);
    }

    public void updateGGAmountUsed(Map param)
    {
        getSqlMapClientTemplate().update("charge.updateGGAmountUsed", param);
        
    }

    public void updateMedioAmountUsed(Map param)
    {
        getSqlMapClientTemplate().update("charge.updateMedioAmountUsed", param);
    }
    
    /**
     * 按编号查询
     */
    public ProjectMedium getProjectMediumByProMedID(int proMedID)
    {
        ProjectMedium projectMedium = (ProjectMedium) getSqlMapClientTemplate().queryForObject("charge.getProjectMedium",
                proMedID);
        return projectMedium;
    }

    public List<Charge> getChargesByProMedID(Integer proMedID)
    {
        
        return getSqlMapClientTemplate().queryForList("charge.getChargesByProMedID", proMedID);
    }
    
    /**
     * 删除费用信息时，还原公共预算剩余额
     * @param condition
     * @return
     */
    public void updateGGAmountUsedByProMedID(Map param)
    {
        getSqlMapClientTemplate().update("charge.updateGGAmountUsedByProMedID", param);
    }

    /**
     * 删除费用信息时，还原预算
     * @param condition
     * @return
     */
    public void updateMedioAmountUsedByProMedID(Map param)
    {
        getSqlMapClientTemplate().update("charge.updateMedioAmountUsedByProMedID", param);
    }

    /**
     * 费用删除时，还原媒体价格表费用总额
     * @param condition
     * @return
     */
    public void updateMedioTotalPayByProMedID(Map param)
    {
        getSqlMapClientTemplate().update("charge.updateMedioTotalPayByProMedID", param);
    }

    /**
     * 根据项目媒介id删除项目媒介费用信息
     * @param condition
     * @return
     */
    public void delChargesbyProMedID(Integer proMedID)
    {
        getSqlMapClientTemplate().update("charge.delChargesbyProMedID", proMedID);
    }
    
    /**
     * 根据项目媒介id取得项目价格信息
     * @param condition
     * @return
     */
    public MediaPrice getMediaPriceByProMedID(Integer proMedID)
    {
        Object obj = null;
        try{
            obj = getSqlMapClientTemplate().queryForObject("charge.getMediaPriceByProMedID", proMedID);
        }catch (Exception e) {
            e.printStackTrace();
        }
        
        return obj == null ? null : (MediaPrice) obj;
    }
}
